using Senparc.Weixin.Entities;
using Senparc.Weixin.MP.AdvancedAPIs.GroupMessage;
using Senparc.Weixin.MP.CommonAPIs;
using System;
namespace Senparc.Weixin.MP.AdvancedAPIs
{
	public static class GroupMessageApi
	{
		public static SendResult SendGroupMessageByGroupId(string accessTokenOrAppId, string groupId, string value, GroupMessageType type, bool isToAll = false, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<SendResult>(delegate(string accessToken)
			{
				BaseGroupMessageDataByGroupId data;
				switch (type)
				{
				case GroupMessageType.mpnews:
					data = new GroupMessageByGroupId_MpNewsData
					{
						filter = new GroupMessageByGroupId_GroupId
						{
							group_id = groupId,
							is_to_all = isToAll
						},
						mpnews = new GroupMessageByGroupId_MediaId
						{
							media_id = value
						},
						msgtype = "mpnews"
					};
					break;
				case GroupMessageType.text:
					data = new GroupMessageByGroupId_TextData
					{
						filter = new GroupMessageByGroupId_GroupId
						{
							group_id = groupId,
							is_to_all = isToAll
						},
						text = new GroupMessageByGroupId_Content
						{
							content = value
						},
						msgtype = "text"
					};
					break;
				case GroupMessageType.voice:
					data = new GroupMessageByGroupId_VoiceData
					{
						filter = new GroupMessageByGroupId_GroupId
						{
							group_id = groupId,
							is_to_all = isToAll
						},
						voice = new GroupMessageByGroupId_MediaId
						{
							media_id = value
						},
						msgtype = "voice"
					};
					break;
				case GroupMessageType.image:
					data = new GroupMessageByGroupId_ImageData
					{
						filter = new GroupMessageByGroupId_GroupId
						{
							group_id = groupId,
							is_to_all = isToAll
						},
						image = new GroupMessageByGroupId_MediaId
						{
							media_id = value
						},
						msgtype = "image"
					};
					break;
				case GroupMessageType.video:
					data = new GroupMessageByGroupId_MpVideoData
					{
						filter = new GroupMessageByGroupId_GroupId
						{
							group_id = groupId,
							is_to_all = isToAll
						},
						mpvideo = new GroupMessageByGroupId_MediaId
						{
							media_id = value
						},
						msgtype = "mpvideo"
					};
					break;
				case GroupMessageType.wxcard:
					data = new GroupMessageByGroupId_WxCardData
					{
						filter = new GroupMessageByGroupId_GroupId
						{
							group_id = groupId,
							is_to_all = isToAll
						},
						wxcard = new GroupMessageByGroupId_WxCard
						{
							card_id = value
						},
						msgtype = "wxcard"
					};
					break;
				default:
					throw new Exception("参数错误。");
				}
				return CommonJsonSend.Send<SendResult>(accessToken, "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={0}", data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static SendResult SendGroupMessageByOpenId(string accessTokenOrAppId, GroupMessageType type, string value, int timeOut = 10000, params string[] openIds)
		{
			return ApiHandlerWapper.TryCommonApi<SendResult>(delegate(string accessToken)
			{
				BaseGroupMessageDataByOpenId data;
				switch (type)
				{
				case GroupMessageType.mpnews:
					data = new GroupMessageByOpenId_MpNewsData
					{
						touser = openIds,
						mpnews = new GroupMessageByOpenId_MediaId
						{
							media_id = value
						},
						msgtype = "mpnews"
					};
					break;
				case GroupMessageType.text:
					data = new GroupMessageByOpenId_TextData
					{
						touser = openIds,
						text = new GroupMessageByOpenId_Content
						{
							content = value
						},
						msgtype = "text"
					};
					break;
				case GroupMessageType.voice:
					data = new GroupMessageByOpenId_VoiceData
					{
						touser = openIds,
						voice = new GroupMessageByOpenId_MediaId
						{
							media_id = value
						},
						msgtype = "voice"
					};
					break;
				case GroupMessageType.image:
					data = new GroupMessageByOpenId_ImageData
					{
						touser = openIds,
						image = new GroupMessageByOpenId_MediaId
						{
							media_id = value
						},
						msgtype = "image"
					};
					break;
				case GroupMessageType.video:
					throw new Exception("发送视频信息请使用SendVideoGroupMessageByOpenId方法。");
				case GroupMessageType.wxcard:
					data = new GroupMessageByOpenId_WxCardData
					{
						touser = openIds,
						wxcard = new GroupMessageByOpenId_WxCard
						{
							card_id = value
						},
						msgtype = "wxcard"
					};
					break;
				default:
					throw new Exception("参数错误。");
				}
				return CommonJsonSend.Send<SendResult>(accessToken, "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={0}", data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static SendResult SendVideoGroupMessageByOpenId(string accessTokenOrAppId, string title, string description, string mediaId, int timeOut = 10000, params string[] openIds)
		{
			return ApiHandlerWapper.TryCommonApi<SendResult>(delegate(string accessToken)
			{
				BaseGroupMessageDataByOpenId data = new GroupMessageByOpenId_MpVideoData
				{
					touser = openIds,
					video = new GroupMessageByOpenId_Video
					{
						title = title,
						description = description,
						media_id = mediaId
					},
					msgtype = "mpvideo"
				};
				return CommonJsonSend.Send<SendResult>(accessToken, "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={0}", data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult DeleteSendMessage(string accessTokenOrAppId, string msgId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				var data = new
				{
					msgid = msgId
				};
				return CommonJsonSend.Send<WxJsonResult>(accessToken, "https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token={0}", data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static SendResult SendGroupMessagePreview(string accessTokenOrAppId, GroupMessageType type, string value, string openId, string wxName = null, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<SendResult>(delegate(string accessToken)
			{
				BaseGroupMessageDataPreview data;
				switch (type)
				{
				case GroupMessageType.mpnews:
					data = new GroupMessagePreview_MpNewsData
					{
						touser = openId,
						towxname = wxName,
						mpnews = new GroupMessagePreview_MediaId
						{
							media_id = value
						},
						msgtype = "mpnews"
					};
					break;
				case GroupMessageType.text:
					data = new GroupMessagePreview_TextData
					{
						touser = openId,
						towxname = wxName,
						text = new GroupMessagePreview_Content
						{
							content = value
						},
						msgtype = "text"
					};
					break;
				case GroupMessageType.voice:
					data = new GroupMessagePreview_VoiceData
					{
						touser = openId,
						towxname = wxName,
						voice = new GroupMessagePreview_MediaId
						{
							media_id = value
						},
						msgtype = "voice"
					};
					break;
				case GroupMessageType.image:
					data = new GroupMessagePreview_ImageData
					{
						touser = openId,
						towxname = wxName,
						image = new GroupMessagePreview_MediaId
						{
							media_id = value
						},
						msgtype = "image"
					};
					break;
				case GroupMessageType.video:
					data = new GroupMessagePreview_MpVideoData
					{
						touser = openId,
						towxname = wxName,
						mpvideo = new GroupMessagePreview_MediaId
						{
							media_id = value
						},
						msgtype = "mpvideo"
					};
					break;
				case GroupMessageType.wxcard:
					throw new Exception("发送卡券息请使用WxCardGroupMessagePreview方法。");
				default:
					throw new Exception("参数错误。");
				}
				return CommonJsonSend.Send<SendResult>(accessToken, "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token={0}", data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static SendResult WxCardGroupMessagePreview(string accessTokenOrAppId, string cardId, string code, string openId, string wxName, string timestamp, string signature, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<SendResult>(delegate(string accessToken)
			{
				BaseGroupMessageDataPreview data = new GroupMessagePreview_WxCardData
				{
					touser = openId,
					towxname = wxName,
					wxcard = new GroupMessagePreview_WxCard
					{
						card_id = cardId,
						card_ext = string.Format("\"code\":\"{0}\",\"openid\":\"{1}\",\"timestamp\":\"{2}\",\"signature\":\"{3}\"", new object[]
						{
							code,
							openId,
							timestamp,
							signature
						})
					},
					msgtype = "wxcard"
				};
				return CommonJsonSend.Send<SendResult>(accessToken, "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token={0}", data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static GetSendResult GetGroupMessageResult(string accessTokenOrAppId, string msgId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<GetSendResult>(delegate(string accessToken)
			{
				var data = new
				{
					msg_id = msgId
				};
				return CommonJsonSend.Send<GetSendResult>(accessToken, "https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token={0}", data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static VideoMediaIdResult GetVideoMediaIdResult(string accessTokenOrAppId, string mediaId, string title, string description, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<VideoMediaIdResult>(delegate(string accessToken)
			{
				string urlFormat = string.Format("https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token={0}", accessToken);
				var data = new
				{
					media_id = mediaId,
					title = title,
					description = description
				};
				return CommonJsonSend.Send<VideoMediaIdResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut, true);
			}, accessTokenOrAppId, true);
		}
	}
}
